package com.lh.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lh.entity.table.CompanyData;
import com.lh.entity.table.DeadLineNodeRecord;
import com.lh.mapper.DeadLineNodeRecordMapper;
import com.lh.service.DeadLineNodeRecordService;
import com.lh.utils.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class DeadLineNodeRecordServiceImpl implements DeadLineNodeRecordService {

    @Autowired
    private DeadLineNodeRecordMapper deadLineNodeRecordMapper;


    @Override
    public Answer<Object> addDeadLineNodeRecord(DeadLineNodeRecord deadLineNodeRecord) {
        try{
            deadLineNodeRecord.setUpdateTime(new Date());
            deadLineNodeRecordMapper.insert(deadLineNodeRecord);
            return Answer.ok("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("添加失败");
        }
    }

    @Override
    public Answer<Object> updateDeadLineNodeRecord(DeadLineNodeRecord deadLineNodeRecord) {
        try{
            Integer id = deadLineNodeRecord.getId();
            if (id == null){
                return Answer.error("id不能为空");
            }
            deadLineNodeRecordMapper.updateById(deadLineNodeRecord);
            return Answer.ok("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("更新失败");
        }
    }

    @Override
    public Answer<Object> deleteDeadLineNodeRecord(DeadLineNodeRecord deadLineNodeRecord) {
        try{
            Integer id = deadLineNodeRecord.getId();
            if (id == null){
                return Answer.error("id不能为空");
            }
            deadLineNodeRecordMapper.deleteById(id);
            return Answer.ok("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("删除失败");
        }
    }

    @Override
    public Answer<Object> queryDeadLineNodeRecordList(DeadLineNodeRecord deadLineNodeRecord) {
        try{
            LambdaQueryWrapper<DeadLineNodeRecord> deadLineNodeRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();

            String fileUrl = deadLineNodeRecord.getFileUrl();
            String progress = deadLineNodeRecord.getProgress();
            String remark = deadLineNodeRecord.getRemark();
            Integer duration = deadLineNodeRecord.getDuration();
            String description = deadLineNodeRecord.getDescription();
            Date endTime = deadLineNodeRecord.getEndTime();
            Date updateTime = deadLineNodeRecord.getUpdateTime();

            if (StrUtil.isNotBlank(fileUrl)){
                deadLineNodeRecordLambdaQueryWrapper.eq(DeadLineNodeRecord::getFileUrl,fileUrl);
            }
            if (StrUtil.isNotBlank(progress)){
                deadLineNodeRecordLambdaQueryWrapper.eq(DeadLineNodeRecord::getProgress,progress);
            }
            if (StrUtil.isNotBlank(remark)){
                deadLineNodeRecordLambdaQueryWrapper.eq(DeadLineNodeRecord::getRemark,remark);
            }
            if (null!=duration){
                deadLineNodeRecordLambdaQueryWrapper.eq(DeadLineNodeRecord::getDuration,duration);
            }
            if (StrUtil.isNotBlank(description)){
                deadLineNodeRecordLambdaQueryWrapper.eq(DeadLineNodeRecord::getDescription,description);
            }
            if (null!=endTime){
                deadLineNodeRecordLambdaQueryWrapper.like(DeadLineNodeRecord::getEndTime,endTime);
            }
            if (null!=updateTime){
                deadLineNodeRecordLambdaQueryWrapper.like(DeadLineNodeRecord::getUpdateTime,updateTime);
            }

            List<DeadLineNodeRecord> list = deadLineNodeRecordMapper.selectList(deadLineNodeRecordLambdaQueryWrapper);
            return Answer.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("查询失败");
        }
    }

    @Override
    public Answer<Object> queryDeadLineNodeRecordPage(JSONObject reqJson) {
        try{
            Integer pageSize = reqJson.getInteger("pageSize");
            Integer pageNum = reqJson.getInteger("pageNum");
            Page<DeadLineNodeRecord> page = new Page<>(pageNum, pageSize);
            LambdaQueryWrapper<DeadLineNodeRecord> deadLineNodeRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();

            String fileUrl = reqJson.getString("fileUrl");
            String progress = reqJson.getString("progress");
            String remark = reqJson.getString("remark");
            Integer duration = reqJson.getInteger("duration");
            String description = reqJson.getString("description");
            Date endTime = reqJson.getDate("endTime");
            Date updateTime = reqJson.getDate("updateTime");

            if (StrUtil.isNotBlank(fileUrl)){
                deadLineNodeRecordLambdaQueryWrapper.eq(DeadLineNodeRecord::getFileUrl,fileUrl);
            }
            if (StrUtil.isNotBlank(progress)){
                deadLineNodeRecordLambdaQueryWrapper.eq(DeadLineNodeRecord::getProgress,progress);
            }
            if (StrUtil.isNotBlank(remark)){
                deadLineNodeRecordLambdaQueryWrapper.eq(DeadLineNodeRecord::getRemark,remark);
            }
            if (null!=duration){
                deadLineNodeRecordLambdaQueryWrapper.eq(DeadLineNodeRecord::getDuration,duration);
            }
            if (StrUtil.isNotBlank(description)){
                deadLineNodeRecordLambdaQueryWrapper.eq(DeadLineNodeRecord::getDescription,description);
            }
            if (null!=endTime){
                deadLineNodeRecordLambdaQueryWrapper.like(DeadLineNodeRecord::getEndTime,endTime);
            }
            if (null!=updateTime){
                deadLineNodeRecordLambdaQueryWrapper.like(DeadLineNodeRecord::getUpdateTime,updateTime);
            }

            Page<DeadLineNodeRecord> deadLineNodeRecordPage =deadLineNodeRecordMapper.selectPage(page,deadLineNodeRecordLambdaQueryWrapper);
            return Answer.ok(deadLineNodeRecordPage);
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("查询失败");
        }
    }
}
